package com.sky.controller.user;

import com.sky.context.BaseContext;
import com.sky.entity.BookingOrder;
import com.sky.mapper.BookingOrderMapper;
import com.sky.mapper.PaymentMapper;
import com.sky.result.Result;
import com.sky.service.PaymentService;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/payment")
@Api(tags="订单支付相关接口")
@CrossOrigin
@Configuration
public class PaymentController {

    @Autowired
    private PaymentService paymentService;


    /**
     * 查询用户的所有订单
     */
    @ApiOperation(value="所有订单订单")
    @GetMapping("/all")
    public Result<List<OrderVO>> getAllOrders() {
        List<OrderVO> orders = paymentService.getAllOrdersByUserId();
        return Result.success(orders);
    }

    /**
     * 查询用户的已支付订单中可以申请取消的订单
     */
    @ApiOperation(value="可申请退款订单")
    @GetMapping("/paid")
    public Result<List<OrderVO>> getPaidOrders() {
        List<OrderVO> paidOrders = paymentService.getPaidOrders();
        return Result.success(paidOrders);
    }




    /**
     * 支付订单
     */
    @ApiOperation(value="支付订单")
    @PostMapping("/pay/{orderId}/{paymentMethod}")
    @Transactional
    public Result<String> payOrder(@PathVariable Long orderId, @PathVariable String paymentMethod) {
        // 调用服务层处理支付逻辑
        paymentService.payOrder(orderId,paymentMethod);
        return Result.success("支付成功");
    }

    /**
     * 取消订单（逻辑删除）0
     */
    @ApiOperation(value="取消订单")
    @PostMapping("/cancel/{orderId}")
    @Transactional
    public Result<String> cancelOrder(@PathVariable Long orderId) {
        paymentService.cancelOrder(orderId);
        // 更新支付记录状态为已取消
        //paymentMapper.updateStatusByOrderId(orderId, "已取消");

        return Result.success("订单已取消");
    }

    /**
     * 申请退款
     */
    @ApiOperation(value="申请退款")
    @PostMapping("/refund/{orderId}")
    @Transactional
    public Result<String> applyRefund(@PathVariable Long orderId) {
        // 更新支付记录状态为退款
        return Result.success(paymentService.refund(orderId));
    }



    /**
     * 申请退款
     */
    @ApiOperation(value="查看可退款金额")
    @PostMapping("//refund-amount/{orderId}/{orderId}")
    @Transactional
    public Result<String> queryRefundableAmount(@PathVariable Long orderId) {
        // 更新支付记录状态为退款
        return Result.success(paymentService.queryRefundableAmount(orderId));
    }


}
